package pe.org.cnl.congreso.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pe.org.cnl.congreso.dao.BaseDAO;
import pe.org.cnl.congreso.service.BaseService;

@Transactional(readOnly = true)
@Service
public abstract class BaseServiceImpl<Entidad, TipoLLave> implements
		BaseService<Entidad, TipoLLave> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8689463786842580735L;
	
	protected static final Log logger = LogFactory.getLog(BaseServiceImpl.class);

	private BaseDAO<Entidad, TipoLLave> baseHibernate;
	
	protected enum TipoOperacion{CREAR,ACTUALIZAR,ELIMINAR}

	protected BaseServiceImpl(BaseDAO<Entidad, TipoLLave> baseHibernate) {
		this.baseHibernate = baseHibernate;
	}

	@Override
	public Entidad obtener(TipoLLave id) {
		return this.baseHibernate.obtener(id);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void actualizar(Entidad object) {
		this.baseHibernate.modificar(object);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void crear(Entidad object) {
				
		this.baseHibernate.crear(object);
		
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void eliminar(Entidad object) {	
		this.baseHibernate.eliminar(object);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void eliminarXId(TipoLLave id) {		
		this.baseHibernate.eliminarXId(id);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void eliminarTodos(List<Entidad> list) {
		this.baseHibernate.eliminarTodos(list);
	}	
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void grabarTodos(List<Entidad> list)	{
		if(list == null)
		{
			//TODO Lanzar una excepcion
		}
		if(list.size() == 0){
			return;
		}
				
		this.baseHibernate.grabarTodos(list);
		
	}
	

	public Integer count(){
		return Integer.parseInt(  this.baseHibernate.count() + "" );
	}


}
